import javalang
from javalang.tree import MethodDeclaration, ClassDeclaration, MethodInvocation


def analyze_java_ast(java_code):
    """
    使用javalang解析Java代码
    """
    try:
        tree = javalang.parse.parse(java_code)
        return tree
    except Exception as e:
        print(f"解析Java代码失败: {e}")
        return None


def find_java_method_calls(java_file_path, target_method_name):
    """
    在Java文件中查找特定方法的调用
    """
    with open(java_file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    tree = analyze_java_ast(content)
    if not tree:
        return []

    method_calls = []

    # 遍历AST查找方法调用
    for path, node in tree:
        if isinstance(node, MethodInvocation):
            if node.member == target_method_name:
                # 获取调用位置信息
                call_info = {
                    'method': node.member,
                    'position': node.position,
                    'arguments': len(node.arguments) if node.arguments else 0,
                    'file': java_file_path
                }
                method_calls.append(call_info)

    return method_calls


def extract_java_entities(java_file_path):
    """
    提取Java文件中的类和方法定义
    """
    with open(java_file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    tree = analyze_java_ast(content)
    if not tree:
        return [], []

    classes = []
    methods = []

    for path, node in tree:
        if isinstance(node, ClassDeclaration):
            classes.append(node.name)
        elif isinstance(node, MethodDeclaration):
            methods.append(node.name)

    return classes, methods

print('11')
print(22)
print(33)